﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using forum.Data;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace forum.ApplicationCode
{
    public class Membership
    {
        public static string UserName
        {
            get
            {
                return GetCookieValue("UserName");
            }
        }



        public static bool Login(string UserName, string Password)
        {

            using (forumDataContext data = new forumDataContext())
            {
                if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
                {
                    HttpCookie cookie = HttpContext.Current.Request.Cookies["forumCookie"];
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }

                // Check if user with that details in the db.
                var User = data.sp_Login(UserName, Password).FirstOrDefault();
                if (User == null)
                {
                    return false;
                }
                else
                {
                    HttpCookie cookie = new HttpCookie("forumCookie");
                    cookie.Values.Add("userName", UserName);
                  //  cookie.Expires = DateTime.Now.AddDays(7);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                    
                   forumConnection.login(UserName, Password);

                    return true;
                }
            }
        }


        public static Boolean IsUserBannedFromForum(int subForumId)
        {
            if (Membership.GetUserId() == 0)
                return false;
            using (forumDataContext data = new forumDataContext())
            {
                int isBanned = data.sp_IsUserBanned(Membership.GetUserId(), subForumId).FirstOrDefault().Column1.Value;
                if (isBanned == 0)
                {
                    return false;
                }
                return true;
            }
        }

        public static string GetUserName()
        {
            String userName = "";

            if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies["forumCookie"];
                userName = cookie["UserName"];
            }
            else
            {
                return "";
            }

            return userName;
        }

        public static Boolean IsLoggedIn()
        {
            Boolean loggedIn = false;

            if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
                loggedIn = true;
            return loggedIn;
        }


        public static int GetUserId()
        {
            int UserId = 0;
            if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
            {

                HttpCookie cookie = HttpContext.Current.Request.Cookies["forumCookie"];
                String userName = cookie["UserName"];
                using (forumDataContext data = new forumDataContext())
                {
                    UserId = data.sp_GetUserIdByUserName(userName).FirstOrDefault().userId;
                }
            }
            return UserId;
        }



        internal static string GetCookieValue(string value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["forumCookie"];
            if (cookie != null && cookie[value] != null)
            {
                return cookie[value];
            }
            else
            {
                return null;
            }
        }

        public static Boolean IsAdmin()
        {
            Boolean isAdmin = false;
            if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
            {
                using (forumDataContext data = new forumDataContext())
                {
                    int userId = GetUserId();
                    int admin = data.sp_IsAdmin(userId).FirstOrDefault().isadmin.Value;
                    if (admin>0)
                        isAdmin = true;

                }
            }
            return isAdmin;
        }

        public static Boolean IsModerator(int forumId)
        {
            Boolean isModerator = false;
            int userId = GetUserId();
            if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
            {

                using (forumDataContext data = new forumDataContext())
                {

                    if (data.sp_IsUserModerator(userId, forumId).FirstOrDefault().isModerator > 0)
                        isModerator = true;
                }
               
                
            }
            return isModerator;
        }

        internal static void LogOut()
        {
            if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies["forumCookie"];
                cookie.Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }

        public static Boolean IsOwnerOfThread(int threadId)
        {
            Boolean isOwner = false;
            int userId = GetUserId();
            if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
            {

                using (forumDataContext data = new forumDataContext())
                {

                    if (data.sp_IsOwnerOfThread(userId, threadId).FirstOrDefault().isOwner > 0)
                        isOwner = true;
                }


            }
            return isOwner;
        }

        public static Boolean IsOwnerOfPost(int postId)
        {
            Boolean isOwner = false;
            int userId = GetUserId();
            if (HttpContext.Current.Request.Cookies["forumCookie"] != null)
            {

                using (forumDataContext data = new forumDataContext())
                {

                    if (data.sp_IsOwnerOfPost(userId, postId).FirstOrDefault().isOwner > 0)
                        isOwner = true;
                }
            }
            return isOwner;
        }





        public static Boolean IsModerator()
        {
            using (forumDataContext data = new forumDataContext())
            {

                if (data.sp_IsModerator(GetUserId()).FirstOrDefault().Column1.Value>0) 
                    return true;
                return false;
            }
        }


    }
}
